<template>
	<view>
		<view class="images">
			<u--image :src="imgurl?imgurl:'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'" width="80px" height="80px" radius="10"></u--image>
		</view>
		<view class="button">
			<button type="primary" :disabled="false" :loading="false" open-type="chooseAvatar" @chooseavatar="onChooseAvatar" >获取头像</button>
		</view>
		<view class="Utext">
			<u-form-item label="昵称">
				<u--input v-model="TextURl" type="nickname" style="border: 1px solid #ccc;border-radius: 1px;height: 80rpx;" />
			</u-form-item>
		</view>
		<view class="button" style="margin-top: 200rpx;">
			<button type="primary" style="background-color: #3C9CFF;width: 300rpx;" @click="submit" :loading="loading">提交</button>
		</view>
		
	</view>
</template>

<script>
	import api from "../../until/api.js"
	export default {
		data(){
			return{
				imgurl:"",
				TextURl:"",
				loading:false
			}
		},
		onLoad() {
			
		},
		methods:{
			submit(){
				if(this.imgurl == "" && this.TextURl == ""){
					uni.showToast({
						title:"昵称头像必填",
						icon:"error",
						duration:2000,
					})
				}else{
					this.loading = true
					let data = {
						imgurl : this.imgurl,
						TextURl : this.TextURl
					}
					api.login(data).then(res=>{
						if(res.data.code == 200){
							uni.setStorageSync('token', res.data.msg);
							this.loading = false
							uni.showToast({
								title:"登录成功",
								icon:"success",
								duration:2000,
								success() {
									uni.navigateBack(-1)
									uni.$emit('update',Object.assign(data,{token:res.data.msg}))
								}
							})
						}
					})
				}
			},
			onChooseAvatar(e){
				let img = e.detail.avatarUrl
				//"data:image/jpeg;base64," + 
				let Base64 = wx.getFileSystemManager().readFileSync(img, 'base64')
				let data = {
					imgurl:Base64
				}
				api.updataImg(data).then(res=>{
					if(res){
						console.log(res)
						this.imgurl = res.data.data
						
					}
				})
			},
			getPhoneNumber(e){
				console.log(e)
			}
		}
	}
</script>

<style lang="scss" scoped>
	view{
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	.images{
		width: 80px;
		padding: 100rpx 0;
		margin: 0px auto;
	}
	.button{
		width: 300rpx;
		margin: 0rpx auto;
	}
	.Utext{
		margin-top: 20rpx;
	}
</style>